﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WarehouseManagement.BusinessLogic.DTO;
using WarehouseManagement.DataAccess;


namespace WarehouseManagement.BusinessLogic.BC
{
    public class CustomerBC : BaseBC
    {
        //private CustomerRepository _custRepo;

        public CustomerBC()
        {
            //_custRepo = new CustomerRepository();
        }

        public IEnumerable<Customer> GetCustomerList()
        {
            return Context.Customers.AsEnumerable();
        }

        private Customer GetEntity(int id)
        {
            return Context.Customers.SingleOrDefault(e => e.Id == id);
        }

        public IEnumerable<CustomerDTO> GetCustomerDTOList()
        {
            return Context.Customers.AsEnumerable()
                .Select(e => e.ToDTO())
                .ToList();
        }

        public void DeleteCustomer(int id)
        {
            var entity = GetEntity(id);
            if (entity != null)
            {
                Context.Customers.DeleteOnSubmit(entity);
                SubmitChanges();
            }
        }

        public void UpdateCustomer(CustomerDTO dto)
        {
            var entity = GetEntity(dto.Id);
            if (entity == null) return;

            entity.Address = dto.Address;
            entity.BankAccount = dto.BankAccount;
            entity.BankName = dto.BankName;
            entity.ContactPerson = dto.ContactPerson;
            entity.Email = dto.Email;
            entity.Fax = dto.Fax;
            entity.FirstName = dto.FirstName;
            entity.IsActive = dto.IsActive;
            entity.LastName = dto.LastName;
            entity.Mobile = dto.Mobile;
            entity.Phone = dto.Phone;
            entity.SkypeAccount = dto.SkypeAccount;
            entity.YahooAccount = dto.YahooAccount;
            SubmitChanges();
        }

        public void AddCustomer(CustomerDTO dto)
        {
            var entity = dto.ToEntity();
            Context.Customers.InsertOnSubmit(entity);
            SubmitChanges();
        }
    }
}
